package com.zhaojonhy.zhaopangwb.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.PageIndicator;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.adapter.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity {

    private SharedPreferences sp; //用来判断是否是第一次登陆
    private SharedPreferences.Editor editor ;
    private boolean isFirst ;

    private ViewPager viewPager ;
    PageIndicator mIndicator;
    private int[] mImgIds = new int[]{
            R.drawable.img_01,
            R.drawable.img_02,
            R.drawable.img_03,
            R.drawable.img_04
    } ;
    private List<ImageView> imageViews = new ArrayList<ImageView>() ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_viewpager);

        //设置SharedPreferences
        sp = getSharedPreferences("isFirst",MODE_PRIVATE) ;
        Log.d("TAG","isFirst-->1"+isFirst) ;
        isFirst = sp.getBoolean("isFirst",true) ;
        Log.d("TAG","isFirst-->2"+isFirst) ;
        if(isFirst){
            initView();
        }else {
            intent2Splash();
        }

    }
    private void initView(){
        //为程序设置是否是第一次进入的标记
        saveTag();
        for(int i=0 ;i< mImgIds.length ;i++){
            int imgId = mImgIds[i] ;
            ImageView imageView = new ImageView(getApplicationContext()) ;
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(imgId);
            imageViews.add(imageView) ;
            Log.d("TAG","SIZE-->"+mImgIds) ;
            if(i== mImgIds.length-1){
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        intent2Splash();
                    }
                });

            }
        }

        viewPager = (ViewPager)findViewById(R.id.viewPager) ;
        viewPager.setAdapter(new MyPagerAdapter(imageViews));

        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);

    }
    private void saveTag() {
        editor = sp.edit();
        editor.putBoolean("isFirst", false);
        editor.commit();
    }

    private void intent2Splash(){
        Intent intent = new Intent(GuideActivity.this,SplashActivity.class) ;
        startActivity(intent);
        finish();
    }
}
